package com.zenitera.bigdata.tableapiflinksql;


import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.TableResult;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;


/**
 * 处理时间 proctime()
 * 声明一个额外的字段来作为处理时间字段
 */
public class Flink03_TimeAttributes_01 {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        tableEnv.executeSql("create table sensor(id string,ts bigint,vc int,pt as PROCTIME()) with(" +
                "'connector' = 'filesystem'," +
                "'path' = 'input/sensor.txt'," +
                "'format' = 'csv'" +
                ")");
        TableResult tableResult = tableEnv.executeSql("select * from sensor");
        tableResult.print();
    }
}

/*
input/sensor.txt
sensor_1,1,10
sensor_1,2,20
sensor_2,4,30
sensor_1,4,400
sensor_2,5,50
sensor_2,6,60
--------------------------------
+----+--------------------------------+----------------------+-------------+-------------------------+
| op |                             id |                   ts |          vc |                      pt |
+----+--------------------------------+----------------------+-------------+-------------------------+
| +I |                       sensor_1 |                    1 |          10 | 2023-04-11 17:13:53.840 |
| +I |                       sensor_1 |                    2 |          20 | 2023-04-11 17:13:53.842 |
| +I |                       sensor_2 |                    4 |          30 | 2023-04-11 17:13:53.842 |
| +I |                       sensor_1 |                    4 |         400 | 2023-04-11 17:13:53.842 |
| +I |                       sensor_2 |                    5 |          50 | 2023-04-11 17:13:53.842 |
| +I |                       sensor_2 |                    6 |          60 | 2023-04-11 17:13:53.842 |
+----+--------------------------------+----------------------+-------------+-------------------------+
 */